package org.dawfinn.droiditarian.listener;

import org.dawfinn.droiditarian.data.Deed;

import android.content.Context;
import android.os.AsyncTask;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;

public class CompletedStatusListener implements OnClickListener, OnMenuItemClickListener {

	private final CompletedAdapter completedAdapter;
	private final Deed deed;

	public CompletedStatusListener(Deed deed, Context context) {
		this.completedAdapter = new CompletedAdapter(deed, context);
		this.deed = deed;
	}

	public void onClick(View arg0) {
		doCompleted();
	}

	public boolean onMenuItemClick(MenuItem arg0) {
		doCompleted();
		return true;
	}

	private void doCompleted() {
		completedAdapter.saveDeedState();
		completedAdapter.uploadCompleteToServer();
		if (deed.isCompleted()) {
			new CompletedAsyncTask().execute();
		}
	}

	class CompletedAsyncTask extends AsyncTask<Boolean, Boolean, Boolean> {

		@Override
		protected Boolean doInBackground(Boolean... arg0) {
			completedAdapter.uploadLocationToServer(null);
			return null;
		}

	}
}
